﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using Lib.Utils;

namespace MS.INV.Reports.Costs
{
    public partial class Costs_Chart_Compare : Lib.Bases.Forms.BaseForm
    {
        DataTable DT1 = new DataTable();
        DataTable DT2 = new DataTable();

        public Costs_Chart_Compare()
        {
            InitializeComponent();

            barYear.EditValue = DateTime.Now.Year;
            barYear2.EditValue = DateTime.Now.Year - 1;
        }

        private void btRef_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                pivot.DataSource = DBLayer.ExecDataTable("Rpt_CostChartCompare", "Command,Year1,Year2", "Detail",
                    Cvert.ToInt16(barYear.EditValue, 0), Cvert.ToInt16(barYear2.EditValue, 0));
                Application.DoEvents();

                DT1 = DBLayer.ExecDataTable("Rpt_CostChartCompare", "Command,Year1,Year2", "ByYear1",
                    Cvert.ToInt16(barYear.EditValue, 0), Cvert.ToInt16(barYear2.EditValue, 0));
                Application.DoEvents();
                DT2 = DBLayer.ExecDataTable("Rpt_CostChartCompare", "Command,Year1,Year2", "ByYear2",
                    Cvert.ToInt16(barYear.EditValue, 0), Cvert.ToInt16(barYear2.EditValue, 0));

                chart.Series[0].DataSource = DT1;
                chart.Series[0].ArgumentDataMember = "Thang";
                chart.Series[0].ValueScaleType = ScaleType.Numerical;
                chart.Series[0].ValueDataMembers.AddRange(new string[] { "TongTien" });
                chart.Series[0].LegendText = Cvert.ToInt16(barYear.EditValue, 0).ToString();

                chart.Series[1].DataSource = DT2;
                chart.Series[1].ArgumentDataMember = "Thang";
                chart.Series[1].ValueScaleType = ScaleType.Numerical;
                chart.Series[1].ValueDataMembers.AddRange(new string[] { "TongTien" });
                chart.Series[1].LegendText = Cvert.ToInt16(barYear2.EditValue, 0).ToString();

            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Load data");
            }
        }

        private void btExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (Exports.Costs.Costs_Chart_Compare cls = new Exports.Costs.Costs_Chart_Compare())
            {
                cls.DoExport(DT1, DT2, Cvert.ToInt16(barYear.EditValue, 0), Cvert.ToInt16(barYear2.EditValue, 0));
            }

            if (System.IO.File.Exists(Application.StartupPath + @"\Export Xls\Costs_Chart_Compare.xls"))
                Lib.Utils.Uti.RunFile(Application.StartupPath + @"\Export Xls\Costs_Chart_Compare.xls");
        }

        private void btClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
    }
}
